<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class De_data_model extends MY_Model {

    function __construct() {
        parent::__construct("data", "id", Array(
            "indicator_id" => "De_indicator_model",
            "source_id" => "De_source_model",
            "form_id" => "De_form_model"
                ), "data_ps");
        $this->delAttribute = "is_deleted";
    }

    public function getByLocInd($locType, $locId, $indId) {
        $this->db->select("id");
        $this->db->from("data");
        $this->db->where("indicator_id", $indId);
        switch ($locType) {
            case "REG" :
                $this->db->from("region_data");
                $this->db->where("region_code", $locId);
                $this->db->where("data_id = id");
                break;
            case "PROV" :
                $this->db->from("provinsi_data");
                $this->db->where("provinsi_code", $locId);
                $this->db->where("data_id = id");
                break;
            case "KAB" :
                $this->db->from("kabupaten_data");
                $this->db->where("kabupaten_code", $locId);
                $this->db->where("data_id = id");
                break;
            case "KEC" :
                $this->db->from("kecamatan_data");
                $this->db->where("kecamatan_code", $locId);
                $this->db->where("data_id = id");
                break;
            case "DESA" :
                $this->db->from("perdesaan_data");
                $this->db->where("perdesaan_code", $locId);
                $this->db->where("data_id = id");
                break;
            case "PUSKESMAS" :
                $this->db->from("puskesmas_data");
                $this->db->where("puskesmas_id", $locId);
                $this->db->where("data_id = id");
                break;
            default :
                $this->db->where("data_id <> data_id");
                break;
        }
        $this->db->order_by("year", "desc");
        $query = $this->db->get();
        $result = $query->result();
        $arrr = Array();
        foreach ($result as $row) {
            $obj = $this->findById($row->id);
            if ($obj != NULL) {
                array_push($arrr, $obj);
            }
        }
        return $arrr;
    }

    protected function read($where = Array(), $fetchForeign = true, $depth = 1, $page = -1, $limit = 10) {
        $result = Array();

        foreach (parent::read($where, $fetchForeign, $depth, $page, $limit) as $row) {
            array_push($result, $row);
        }

        return $result;
    }

    public function create($formId, $year, $month, $sourceId, $dataLevel, $locId, $indicatorId, $value) {
        $dataId = 0;
        $this->load->model("De_indicator_model");
        $indicatorObj = $this->De_indicator_model->findById($indicatorId);
        if ($indicatorObj != NULL) {
            $this->load->model("De_source_model");
            $source = $this->De_source_model->findById($sourceId);
            if ($source != NULL) {
                switch ($dataLevel) {
                    case "REG" :
                        $query = $this->db->get_where("region", Array("code" => $locId));
                        $locObj = $query->row();
                        break;
                    case "PROV" :
                        $query = $this->db->get_where("provinsi", Array("code" => $locId));
                        $locObj = $query->row();
                        break;
                    case "KAB" :
                        $query = $this->db->get_where("kabupaten", Array("code" => $locId));
                        $locObj = $query->row();
                        break;
                    case "KEC" :
                        $query = $this->db->get_where("kecamatan", Array("code" => $locId));
                        $locObj = $query->row();
                        break;
                    case "DESA" :
                        $query = $this->db->get_where("perdesaan", Array("code" => $locId));
                        $locObj = $query->row();
                        break;
                    case "PUSKESMAS" :
                        $query = $this->db->get_where("puskesmas", Array("id" => $locId));
                        $locObj = $query->row();
                        break;
                }
                if ($locObj != NULL) {
                    $this->db->insert("data", Array(
                        "value" => $value,
                        "year" => $year,
                        "month" => $month,
                        "indicator_id" => $indicatorObj->id,
                        "source_id" => $source->id,
                        "form_id" => $formId,
                        "is_deleted" => false
                    ));
                    $dataId = $this->db->insert_id();
                }
            }
        }
        if ($dataId > 0) {
            switch ($dataLevel) {
                case "REG" :
                    $this->db->insert("region_data", Array(
                        "region_code" => $locId,
                        "data_id" => $dataId
                    ));
                    break;
                case "PROV" :
                    $this->db->insert("provinsi_data", Array(
                        "provinsi_code" => $locId,
                        "data_id" => $dataId
                    ));
                    break;
                case "KAB" :
                    $this->db->insert("kabupaten_data", Array(
                        "kabupaten_code" => $locId,
                        "data_id" => $dataId
                    ));
                    break;
                case "KEC" :
                    $this->db->insert("kecamatan_data", Array(
                        "kecamatan_code" => $locId,
                        "data_id" => $dataId
                    ));
                    break;
                case "DESA" :
                    $this->db->insert("perdesaan_data", Array(
                        "perdesaan_code" => $locId,
                        "data_id" => $dataId
                    ));
                    break;
                case "PUSKESMAS" :
                    $this->db->insert("puskesmas_data", Array(
                        "puskesmas_id" => $locId,
                        "data_id" => $dataId
                    ));
                    break;
            }
        }
        return $dataId;
    }

}